//
//  HMTabBar.m
//  weibo
//
//  Created by apple on 2016/11/13.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMTabBar.h"

@interface HMTabBar ()
@property(nonatomic,strong)UIButton *addBtn;

@end

@implementation HMTabBar

-(UIButton *)addBtn{
    if (!_addBtn) {
        _addBtn = [UIButton buttonWithType:0];
//        _addBtn.bounds = CGRectMake(0, 0, self.bounds.size.width/5, self.bounds.size.height);
//        _addBtn.center = CGPointMake(self.center.x, self.bounds.size.height/2);
        
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_addBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateSelected];
        
        [_addBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_addBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateSelected];
    }
    return _addBtn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview: self.addBtn];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    NSArray<UIButton *>* btnArr = self.subviews;
    CGFloat w = self.bounds.size.width/5;
    CGFloat h = self.bounds.size.height;
    
//    for (int i = 0; i < btnArr.count; i++) {
//        UIButton *btn = btnArr[i];
//        btn.frame = CGRectMake(i*w, 0, w, h);
//        
//    }
    int count = 0;
    
    for (UIButton *btn in btnArr) {
        if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            btn.frame = CGRectMake(count * w, 0, w, h);
            count += (count == 1 ? 2 : 1);
            
        }

   
    }
 
    [self.addBtn addTarget:self action:@selector(clickAddBtn:) forControlEvents:UIControlEventTouchUpInside];
    _addBtn.bounds = CGRectMake(0, 0, self.bounds.size.width/5, self.bounds.size.height);
            _addBtn.center = CGPointMake(self.center.x, self.bounds.size.height/2);
}

-(void)clickAddBtn:(UIButton *)btn{

    if (self.addBtnBlock) {
        self.addBtnBlock();
    }
}



@end
